﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyDLL;
using MyWinFormDLL;

namespace SQLCommandTransformer
{
    public partial class Form1 : Form
    {
        string richTextBox_Text_Temp = string.Empty;
        string[] selections = new string[]
        {
            "回车转|",
            "C#文本代码转换成单行",
            "SQL命令转换成单行",
            "正则表达式转换为C#文本代码",
            "JSON字符串转换为C#文本代码",
            "JSON字符串转单行",
            "从PDF复制文字删除回车和空格（中文）",
            "从PDF复制文字删除回车和空格（英文）",
            "从Goolge翻译到变量名（首字母均大写）",
            "从Goolge翻译到变量名（驼峰命名法）",
            "把中文字符替换为正常字符",
            "转为LaTex正文文字",
            "转为LaTex表格文字",
            "HTMLChar2EntityName",
            "HTMLEntityName2Char",
            "HTMLChar2EntityNumber",
            "HTMLEntityNumber2Char",
        };
        public Form1()
        {
            InitializeComponent();
            WinFormToolkit.ComboBoxUtility.ComboBox_ToolTip_Class comboBox_ToolTip_Class1 = new WinFormToolkit.ComboBoxUtility.ComboBox_ToolTip_Class(comboBox1, toolTip1);
            WinFormToolkit.ComboBoxUtility.RefreshItemsByStringArray(comboBox1, selections);
        }

        //private Selections selections;

        private void button1_Click(object sender, EventArgs e)
        {
            string tempStr = richTextBox1.Text;
            richTextBox_Text_Temp = richTextBox1.Text;
            switch (comboBox1.Text)
            {
                case "回车转|":
                    tempStr = tempStr.Replace(((char)10).ToString(), @"|");
                    tempStr = tempStr.TrimEnd('|');
                    tempStr = tempStr.Replace(((char)9).ToString(), "");
                    richTextBox1.Text = tempStr;
                    break;
                case "C#文本代码转换成单行":
                    tempStr = tempStr.Replace(((char)10).ToString(), @"\n");
                    richTextBox1.Text = tempStr;
                    break;
                case "SQL命令转换成单行":                    
                    tempStr = tempStr.Replace(((char)10).ToString(), " ");
                    richTextBox1.Text = Regex_Common.RegexReplace(tempStr, "( )+", " ");
                    break;
                case "正则表达式转换为C#文本代码":
                    tempStr = tempStr.Replace(((char)92).ToString(), ((char)92).ToString() + ((char)92).ToString()).Replace(((char)34).ToString(), ((char)92).ToString() + ((char)34).ToString());
                    richTextBox1.Text = tempStr;
                    break;
                case "JSON字符串转换为C#文本代码":
                    tempStr = tempStr.Replace(((char)10).ToString(), "");
                    tempStr = Regex_Common.RegexReplace(tempStr, "\\{ +", "{");
                    tempStr = Regex_Common.RegexReplace(tempStr, " +\\{", "{");
                    tempStr = Regex_Common.RegexReplace(tempStr, "\\} +", "}");
                    tempStr = Regex_Common.RegexReplace(tempStr, " +\\}", "}");
                    tempStr = Regex_Common.RegexReplace(tempStr, ", +", ",");
                    tempStr = Regex_Common.RegexReplace(tempStr, " +,", ",");
                    tempStr = Regex_Common.RegexReplace(tempStr, ": +", ":");
                    tempStr = Regex_Common.RegexReplace(tempStr, " +:", ":");
                    tempStr = Regex_Common.RegexReplace(tempStr, "\\[ +", "[");
                    tempStr = Regex_Common.RegexReplace(tempStr, " +\\[", "[");
                    tempStr = Regex_Common.RegexReplace(tempStr, "\\] +", "]");
                    tempStr = Regex_Common.RegexReplace(tempStr, " +\\]", "]");
                    tempStr = tempStr.Replace(((char)92).ToString(), ((char)92).ToString() + ((char)92).ToString()).Replace(((char)34).ToString(), ((char)92).ToString() + ((char)34).ToString());
                    richTextBox1.Text = tempStr;
                    break;
                case "JSON字符串转单行":
                    tempStr = tempStr.Replace(((char)10).ToString(), "");
                    tempStr = Regex_Common.RegexReplace(tempStr, "\\{ +", "{");
                    tempStr = Regex_Common.RegexReplace(tempStr, " +\\{", "{");
                    tempStr = Regex_Common.RegexReplace(tempStr, "\\} +", "}");
                    tempStr = Regex_Common.RegexReplace(tempStr, " +\\}", "}");
                    tempStr = Regex_Common.RegexReplace(tempStr, ", +", ",");
                    tempStr = Regex_Common.RegexReplace(tempStr, " +,", ",");
                    tempStr = Regex_Common.RegexReplace(tempStr, ": +", ":");
                    tempStr = Regex_Common.RegexReplace(tempStr, " +:", ":");
                    tempStr = Regex_Common.RegexReplace(tempStr, "\\[ +", "[");
                    tempStr = Regex_Common.RegexReplace(tempStr, " +\\[", "[");
                    tempStr = Regex_Common.RegexReplace(tempStr, "\\] +", "]");
                    tempStr = Regex_Common.RegexReplace(tempStr, " +\\]", "]");
                    richTextBox1.Text = tempStr;
                    break;
                case "从PDF复制文字删除回车和空格（中文）":
                    tempStr = tempStr.Replace(((char)10).ToString(), "");
                    tempStr = Regex_Common.RegexReplace(tempStr, "(?<=[^A-Za-z]) {1,}(?=[^A-Za-z])", "");
                    richTextBox1.Text = tempStr;
                    break;
                case "从PDF复制文字删除回车和空格（英文）":
                    tempStr = tempStr.Replace(((char)10).ToString(), " ");
                    tempStr = Regex_Common.RegexReplace(tempStr, " {2,}", " ");
                    richTextBox1.Text = tempStr;
                    break;
                case "从Goolge翻译到变量名（首字母均大写）":
                    tempStr = Regex_Common.RegexReplace(tempStr, " {2,}", " ");
                    richTextBox1.Text = StringUtility.InitialsToUpper(tempStr);
                    break;
                case "从Goolge翻译到变量名（驼峰命名法）":
                    tempStr = Regex_Common.RegexReplace(tempStr, " {2,}", " ");
                    richTextBox1.Text = StringUtility.InitialsToUpperHump(tempStr);
                    break;
                case "把中文字符替换为正常字符":
                    for (int i = 0; i <= (int)'z'-(int)'+'; i++)
                    {
                        tempStr = tempStr.Replace((char)(65291 + i), (char)((int)'+' + i));
                    }
                    richTextBox1.Text = tempStr;
                    break;
                case "转为LaTex正文文字":
                    richTextBox1.Text = ExcelToLaTeX.RenderSpecialChar_Txt(tempStr);
                    break;
                case "转为LaTex表格文字":
                    richTextBox1.Text = ExcelToLaTeX.RenderSpecialChar_Tab(tempStr);
                    break;
                case "HTMLChar2EntityName":
                    richTextBox1.Text = MyDLL.HtmlCharConverter.Converter.Instance.CharacterStr2EntityNameStr(tempStr);
                    break;
                case "HTMLEntityName2Char":
                    richTextBox1.Text = MyDLL.HtmlCharConverter.Converter.Instance.EntityNameStr2CharacterStr(tempStr);
                    break;
                case "HTMLChar2EntityNumber":
                    richTextBox1.Text = MyDLL.HtmlCharConverter.Converter.Instance.CharacterStr2EntityNumberStr(tempStr);
                    break;
                case "HTMLEntityNumber2Char":
                    richTextBox1.Text = MyDLL.HtmlCharConverter.Converter.Instance.EntityNumberStr2CharacterStr(tempStr);
                    break;
                default:
                    break;
            }
        }

        private void button_Undo_Click(object sender, EventArgs e)
        {
            if (richTextBox_Text_Temp!=string.Empty)
                richTextBox1.Text = richTextBox_Text_Temp;
        }

        private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.Z)
                richTextBox1.Text = richTextBox_Text_Temp;
        }
    }
}
